package com.neegix.log.exceptionLog.infrastructure.repository.impl;

import com.neegix.application.query.NebulaSQL;
import com.neegix.base.PageVO;
import com.neegix.log.exceptionLog.application.assembler.ExceptionLogAssembler;
import com.neegix.log.exceptionLog.application.cqrs.query.ExceptionLogQueryRepository;
import com.neegix.log.exceptionLog.application.cqrs.query.condition.ExceptionLogWhereGroup;
import com.neegix.log.exceptionLog.application.dto.ExceptionLogDTO;
import com.neegix.log.exceptionLog.interfaces.vo.ExceptionLogVO;
import com.neegix.log.exceptionLog.infrastructure.repository.convert.ExceptionLogConverter;
import com.neegix.log.exceptionLog.infrastructure.repository.dataobject.ExceptionLogDO;
import com.neegix.log.exceptionLog.infrastructure.repository.mapper.ExceptionLogMapper;
import com.neegix.log.exceptionLog.infrastructure.repository.mapper.customized.ExceptionLogCustomizedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-12-19 15:04:54
  */

@Repository
public class ExceptionLogQueryRepositoryImpl implements ExceptionLogQueryRepository {

    @Autowired
    private ExceptionLogMapper exceptionLogMapper;

    @Autowired
    private ExceptionLogCustomizedMapper exceptionLogCustomizedMapper;

    @Override
    public Optional<ExceptionLogVO> findById(Long id) {
        ExceptionLogDO exceptionLogDO = exceptionLogMapper.selectByPrimaryKey(id);
        return Optional.ofNullable(ExceptionLogAssembler.INSTANCE.covertVO(ExceptionLogConverter.INSTANCE.covertDTO(exceptionLogDO)));
    }

    @Override
    public Integer selectCount(List<Long> ids) {
        return exceptionLogCustomizedMapper.selectCountByIds(ids);
    }

    @Override
    public PageVO<ExceptionLogVO> findPage(Integer currentPage, Integer pageSize, ExceptionLogDTO exceptionLogDTO) {
        NebulaSQL nebulaSQL = new NebulaSQL();
        nebulaSQL.createWhereGroups(ExceptionLogWhereGroup.class)
           .andIdEqualTo(exceptionLogDTO.getId())
           .andCreateTimeBetween(exceptionLogDTO.getStartCreateTime(),exceptionLogDTO.getEndCreateTime())
           .andUpdateTimeBetween(exceptionLogDTO.getStartUpdateTime(),exceptionLogDTO.getEndUpdateTime())
           .andCreateUserEqualTo(exceptionLogDTO.getCreateUser())
           .andUpdateUserEqualTo(exceptionLogDTO.getUpdateUser())
           .andUserLikeTo(exceptionLogDTO.getUser())
           .andModuleLikeTo(exceptionLogDTO.getModule())
           .andTypeEqualTo(exceptionLogDTO.getType())
           .andDescriptionLikeTo(exceptionLogDTO.getDescription())
           .andUriLikeTo(exceptionLogDTO.getUri())
           .andFullNameLikeTo(exceptionLogDTO.getFullName())
           .andExceptionLikeTo(exceptionLogDTO.getException());
           
        nebulaSQL.setPager(currentPage, pageSize);
        List<ExceptionLogDO> result = exceptionLogMapper.selectList(nebulaSQL);
        Long total = exceptionLogMapper.selectCount(nebulaSQL);
        PageVO<ExceptionLogVO> page = new PageVO<>(currentPage, pageSize);
        page.setTotal(total);
        page.setResult(ExceptionLogAssembler.INSTANCE.covertVO(ExceptionLogConverter.INSTANCE.covertDTOS(result)));
        return page;
    }
}
